package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.config.annotation.RequiresPermissions;
import com.zsr.example.entity.CrmBanner;
import com.zsr.example.service.CrmBannerService;
import com.zsr.example.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import static com.zsr.example.util.constants.SysLogConstants.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-06-17
 */
@RestController
@RequestMapping("/banner")
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @RequiresPermissions("banner:list")
    @GetMapping("pageBanner/{page}/{limit}")
    public JSONObject pageBanner(@PathVariable long page, @PathVariable long limit){
        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        QueryWrapper<CrmBanner> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        queryWrapper.orderByDesc("is_show");
        bannerService.page(pageBanner,queryWrapper);
        return CommonUtil.successPage(pageBanner.getTotal(),pageBanner.getRecords());
    }



    /**
     * 添加banner
     * @return
     */
    @RequiresPermissions("banner:add")
    @CacheEvict(value = "banner",allEntries=true,cacheManager="redisCacheManager")//清空
    @PostMapping("addBanner")
    public JSONObject addBanner(@RequestBody CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return CommonUtil.success();
    }

    @OptLog(optType = UPDATE,optDesc = "修改轮播图")
    @RequiresPermissions("banner:update")
    @CacheEvict(value = "banner",allEntries=true,cacheManager="redisCacheManager")//清空
    @PutMapping("update")
    public JSONObject updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return CommonUtil.success();
    }

    @OptLog(optType = DELETE,optDesc = "删除轮播图")
    @RequiresPermissions("banner:delete")
    @CacheEvict(value = "banner",allEntries=true,cacheManager="redisCacheManager")//清空
    @DeleteMapping("{id}")
    public JSONObject remove(@PathVariable String id) {
        bannerService.removeById(id);
        return CommonUtil.success();
    }
}

